package com.framework.dubbo.producer.file2html;


import com.framework.dubbo.producer.config.Constants;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author xgj
 * @description: csv文件预览
 *
 * @date 2020/10/16 11:34
 */
public class CsvFilePreviewUtil extends AbstractFilePreviewTools {

    @Override
    public String convert2Html(String path) {
        return csv2Html(path);
    }

    @Override
    public String convert2Html(InputStream in, String ext) {
        return csv2Html(in);
    }

    @Override
    public String convert2Str(InputStream in, String ext) {
        return csv2Str(in);
    }

    /**
     * csv转换成html
     * @throws IOException
     */
    public static String csv2Html(String path) {
        File f = new File(path);
        if (!f.exists()) {
            return String.format("文件[%s]不存在", path);
        } else {
            try (InputStream in = new FileInputStream(f)) {
                return csv2Html(in);
            } catch (Exception e) {
                e.printStackTrace();
                return String.format("文件[%s]读取失败", path);
            }
        }
    }
    /**
     * 读去csv文件为html格式
     * @return
     */
    public static String csv2Html(InputStream in) {
        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        List<Map<String, Object>> csvData = CsvUtils.getCsvData(in);
        sb.append("<head>");
        sb.append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />");
        sb.append("</head>");
        sb.append("<body text='#000000'>");
        sb.append("<table style='border-collapse:collapse;" +
                "table-layout:fixed;word-break:break-all;width:100%;'>");
        sb.append("<tr height='48.00pt'>");

        Map<String, Object> header = csvData.remove(0);
        String[] headerArr = new String[header.size()];
        for (Map.Entry<String, Object> entry : header.entrySet()) {
            String key = entry.getKey();
            int index = (int)entry.getValue();
            headerArr[index] = key;
        }
        for (String column : headerArr) {
            sb.append("<td align='center' valign='center' " +
                    "style='font-weight:700;font-size: 11pt;font-family: 微软雅黑;" +
                    "background-color:#F5F7F9;border:solid #C3CBDD 1px;' >");
            sb.append(column);
            sb.append("</td>");
        }
        sb.append("</tr>");

        for (Map<String, Object> map : csvData) {
            sb.append("<tr>");
            for (String column : headerArr) {
                Object o = map.get(column);
                String value = "";
                if (o != null) {
                    value = o.toString();
                }
                sb.append("<td align='center' valign='center' style='border:solid #C3CBDD 1px;'>");
                sb.append(value);
                sb.append("</td>");
            }
            sb.append("</tr>");
        }
        sb.append("</table>");
        sb.append("</body>");
        sb.append("</html>");
        return sb.toString();
    }

    /**
     * 读去csv文件为html格式
     * @return
     */
    public static String csv2Str(InputStream in) {
        StringBuffer sb = new StringBuffer();
        List<Map<String, Object>> csvData = CsvUtils.getCsvData(in);
        Map<String, Object> header = csvData.remove(0);
        String[] headerArr = new String[header.size()];
        for (Map.Entry<String, Object> entry : header.entrySet()) {
            String key = entry.getKey();
            int index = (int)entry.getValue();
            headerArr[index] = key;
        }
        for (String column : headerArr) {
            sb.append(column);
            sb.append(Constants.WITHE_SPACE);
        }
        sb.append(Constants.ENTER);

        for (Map<String, Object> map : csvData) {
            for (String column : headerArr) {
                Object o = map.get(column);
                String value = "";
                if (o != null) {
                    value = o.toString();
                }
                sb.append(value);
                sb.append(Constants.WITHE_SPACE);
            }
            sb.append(Constants.ENTER);
        }
        return sb.toString();
    }

    public static void main(String[] args) throws FileNotFoundException {
        File sourceFile = new File("C:\\Users\\ZHAOJIAN\\Desktop\\测试文件\\t.csv");
        FileInputStream fis = new FileInputStream(sourceFile);
        System.out.println(new CsvFilePreviewUtil().convert2Str(fis, "csv"));
    }

}
